package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;

import com.bumptech.glide.BitmapTypeRequest;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.ConversationVH;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.iml.RongUserInfoProvider;
import com.centaline.androidsalesblog.model.businissobject.RongUserInfoBo;
import com.centaline.androidsalesblog.utils.DateUtil;
import com.centanet.framework.base.BaseAdapter1;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.glide.RoundImageViewTarget;

import java.util.ArrayList;

import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.MessageContent;
import io.rong.message.ImageMessage;
import io.rong.message.TextMessage;

/**
 * 会话
 * Created by guilin on 16/3/9.
 */
public class ConversationAdapter extends BaseAdapter1<ConversationVH> {

    private final BitmapTypeRequest<String> mBitmapTypeRequest;
    private final ArrayList<Conversation> list;
    private final SparseArray<RongUserInfoBo> rongUserInfoBoSparseArray = new SparseArray<>();
    private final RecyclerViewItemOnClickListener<Conversation> itemOnClickListener;
    private RongUserInfoProvider rongUserInfoProvider;

    public ConversationAdapter(Context context, BitmapTypeRequest<String> mBitmapTypeRequest,
                               ArrayList<Conversation> list,
                               RecyclerViewItemOnClickListener<Conversation> itemOnClickListener) {
        super(context);
        this.mBitmapTypeRequest = mBitmapTypeRequest;
        this.list = list;
        this.itemOnClickListener = itemOnClickListener;
    }

    public void setRongUserInfoProvider(RongUserInfoProvider rongUserInfoProvider) {
        this.rongUserInfoProvider = rongUserInfoProvider;
    }

    /**
     * 删除
     */
    public void delete(int position) {
        if (rongUserInfoBoSparseArray.size() > 0) {
            int index = rongUserInfoBoSparseArray.indexOfKey(list.get(position).getTargetId().hashCode());
            if (index != -1) {
                rongUserInfoBoSparseArray.removeAt(index);
            }
        }
        list.remove(position);
        notifyItemRemoved(position);
    }

    /**
     * 加入用户信息
     */
    public void putUserInfo(int position, RongUserInfoBo rongUserInfoBo) {
        rongUserInfoBoSparseArray.put(rongUserInfoBo.getUserId().hashCode(), rongUserInfoBo);
        notifyItemChanged(position);
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_conversation;
    }

    @Override
    protected ConversationVH viewHolder(View view) {
        return new ConversationVH(view);
    }

    @Override
    public void onBindViewHolder(ConversationVH holder, int position) {
        final Conversation conversation = list.get(position);
        int index = rongUserInfoBoSparseArray.indexOfKey(conversation.getTargetId().hashCode());
        if (index < 0) {//不存在
            if (rongUserInfoProvider != null) {
                rongUserInfoProvider.provide(conversation.getTargetId(), holder.getAdapterPosition());
            }
        } else {//存在
            RongUserInfoBo rongUserInfoBo = rongUserInfoBoSparseArray.valueAt(index);
            holder.atv_name.setText(rongUserInfoBo.getNickName());
            if (GlideLoad.needLoad(true)) {
                mBitmapTypeRequest
                        .load(rongUserInfoBo.getPortraitUrl())
                        .placeholder(R.drawable.ic_staff_default)
                        .error(R.drawable.ic_staff_default)
                        .centerCrop()
                        .into(new RoundImageViewTarget(holder.img_user));
            } else {
                mBitmapTypeRequest
                        .load("")
                        .placeholder(R.drawable.ic_staff_default)
                        .error(R.drawable.ic_staff_default)
                        .centerCrop()
                        .into(new RoundImageViewTarget(holder.img_user));
            }
        }
        MessageContent messageContent = conversation.getLatestMessage();
        if (messageContent != null) {
            if (messageContent instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) messageContent;
                holder.atv_content.setText(textMessage.getContent());
            } else if (messageContent instanceof ImageMessage) {
                holder.atv_content.setText("[图片]");
            } else {
                holder.atv_content.setText(null);
            }
        } else {
            holder.atv_content.setText(null);
        }
        long sendWhen = conversation.getSentTime();
        long receivedWhen = conversation.getReceivedTime();
        holder.atv_date.setText(DateUtil.timeRule(sendWhen > receivedWhen ? sendWhen : receivedWhen));
        holder.img_point.setVisibility(conversation.getUnreadMessageCount() > 0 ? View.VISIBLE : View.GONE);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemOnClickListener.onItemOnClick(conversation, 0);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
